/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.excel;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.enums.library.DifficultyLevel;
import org.springblade.common.enums.library.QuestionType;
import org.springblade.common.exception.TrainingServiceException;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.modules.library.entity.QuestionBankEntity;
import org.springblade.modules.library.entity.QuestionOptionEntity;
import org.springblade.modules.library.excel.QuestionBankExcel;
import org.springblade.modules.library.service.IQuestionBankService;
import org.springblade.modules.library.service.IQuestionOptionService;
import org.springblade.modules.material.entity.PaperDetailEntity;
import org.springblade.modules.material.entity.PaperEntity;
import org.springblade.modules.material.service.IPaperDetailService;
import org.springblade.modules.material.service.IPaperService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目数据导入类
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class PaperImporter implements ExcelImporter<PaperImportExcel> {

	private final IQuestionBankService questionBankService;
	private final IQuestionOptionService questionOptionService;
	private final IPaperService paperService;
	private final IPaperDetailService paperDetailService;

	@Override
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public void save(List<PaperImportExcel> dataList) {
		if (dataList.size()>1000)throw new ServiceException("导入条数不可超过1000条");
		String version= CommonUtil.getImportVersion();
		PaperEntity paper=new PaperEntity();
		String paperName=dataList.get(0).getPaperName();
		if (StringUtils.isEmpty(paperName)) paperName=dataList.get(0).getCategoryFirst();
		if (StringUtils.isEmpty(paperName)) paperName=version;
		paper.setPaperName(paperName);
		paper.setIsNeedReview(0);  //默认是不需要阅卷，如果有简答题 下面会设置
		paperService.save(paper);

		List<PaperDetailEntity> paperDetailEntityList=new ArrayList<>();

		for (PaperImportExcel data:dataList){
			List<QuestionOptionEntity> questionOptionEntityList=new ArrayList<>();
			QuestionBankEntity questionBankEntity=new QuestionBankEntity();
			questionBankEntity.setVersion(version);
			questionBankEntity.setContent(data.getContent());
			questionBankEntity.setDifficultyLevel(DifficultyLevel.getCodeByValue(data.getDifficultyLevel()));
			questionBankEntity.setQuestionProperties(data.getQuestionProperties());
			questionBankEntity.setQuestionNumber(data.getQuestionNumber());
			questionBankEntity.setBankName(data.getBankName());
			char[] array =null;
			if (null!=data.getRight())array =data.getRight().toCharArray();
			if (null==data.getQuestionType())throw new TrainingServiceException("题目类型不能为空");
			switch (data.getQuestionType()){
				case "单选题" :
					questionBankEntity.setQuestionType(QuestionType.SINGLE.getCode());
					questionBankEntity.setPaperTypeTitle(QuestionType.SINGLE.getValue());
					break;
				case "多选题" :
					questionBankEntity.setQuestionType(QuestionType.MULTIPLE.getCode());
					questionBankEntity.setPaperTypeTitle(QuestionType.MULTIPLE.getValue());
					break;
				case "是非题" :
					questionBankEntity.setQuestionType(QuestionType.JUDGE.getCode());
					questionBankEntity.setPaperTypeTitle(QuestionType.JUDGE.getValue());
					break;
				case "判断题" :
					questionBankEntity.setQuestionType(QuestionType.JUDGE.getCode());
					questionBankEntity.setPaperTypeTitle(QuestionType.JUDGE.getValue());
					break;
				case "简答题" :
					questionBankEntity.setQuestionType(QuestionType.SHORT.getCode());
					questionBankEntity.setPaperTypeTitle(QuestionType.SHORT.getValue());
					break;
				default:
					throw new TrainingServiceException("题目类型不正确,应为单选题、多选题、判断题、简答题的其中一种");
			}

			//设置题目类别，默认为1练习题。如果值为考试题,则为2
			int affiliationType=2;//导入试卷 默认为考试题
			questionBankEntity.setAffiliationType(affiliationType);
			if (StringUtils.isNotEmpty(data.getCategoryFirst()))questionBankEntity.setCategoryFirst(data.getCategoryFirst());
			if (StringUtils.isNotEmpty(data.getCategorySecond()))questionBankEntity.setCategorySecond(data.getCategorySecond());

			//保存试题
			questionBankService.save(questionBankEntity);

			//试卷详情
			PaperDetailEntity paperDetailEntity=new PaperDetailEntity();
			paperDetailEntity.setPid(paper.getId());
			if (null!=questionBankEntity.getQuestionType() && QuestionType.SHORT.getCode().intValue()==questionBankEntity.getQuestionType().intValue())paper.setIsNeedReview(1);
			paperDetailEntity.setQuestionType(questionBankEntity.getQuestionType());
			paperDetailEntity.setQuestionBankId(questionBankEntity.getId());
			paperDetailEntity.setDifficultyLevel(questionBankEntity.getDifficultyLevel());
			paperDetailEntity.setContent(questionBankEntity.getContent());
			paperDetailEntityList.add(paperDetailEntity);

			//保存选项
			getQuestionOptionEntity(data.getOptionA(),questionBankEntity.getId(),array,'A',version,questionOptionEntityList);
			getQuestionOptionEntity(data.getOptionB(),questionBankEntity.getId(),array,'B',version,questionOptionEntityList);
			getQuestionOptionEntity(data.getOptionC(),questionBankEntity.getId(),array,'C',version,questionOptionEntityList);
			getQuestionOptionEntity(data.getOptionD(),questionBankEntity.getId(),array,'D',version,questionOptionEntityList);
			questionOptionService.saveBatch(questionOptionEntityList);
		}

		paperService.updateById(paper);
		paperDetailService.saveBatch(paperDetailEntityList);
	}

	public void getQuestionOptionEntity(String content,Long questionId,char[] rightArray,char option,String version,List<QuestionOptionEntity> questionOptionEntityList){
		QuestionOptionEntity optionEntity=new QuestionOptionEntity();
		if(null ==content) return ;
		optionEntity.setOption(content);
		optionEntity.setVersion(version);
		optionEntity.setQuestionBankId(questionId);
		//辨别是否正确答案
		if (questionIsRight(rightArray,option)) optionEntity.setIsRight(1);
		else optionEntity.setIsRight(0);
		questionOptionEntityList.add(optionEntity);
	}



	/**
	 * 判断该选项是否为正确选项
	 */
	public boolean questionIsRight(char[] array,char option){
		if (null==array) return false;
		for (char c:array){
			if (c==option) return true;
		}
		return false;
	}

}
